package org.cklxh.waptime;

import java.io.OutputStreamWriter;

import org.cklxh.widget.DigitalClock;
import org.waptime.R;

import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;

public class SetTimeOnClickListener implements OnClickListener {

	@Override
	public void onClick(View view) {
		
		WaptimeActivity waptimeActivity=WaptimeActivity.getActivityObject();
		//Context context=waptimeActivity.getBaseContext();
		DigitalClock internetTime_DC=(DigitalClock)waptimeActivity.findViewById(R.id.internetTime_DC);
		long time=internetTime_DC.getTime();
		//修改系统时间，需要ROOT权限。
		Runtime runtime = Runtime.getRuntime();
		try {
			int exitVal = 0;
			Process proc = runtime.exec("su");
			final OutputStreamWriter out = new OutputStreamWriter(proc.getOutputStream());
			// 设置时间
			out.write("date "+time/1000);
			// 写入硬件，或许不需要。
			//out.write("\nclock -w \n");
			//out.flush();
			// 退出
			out.write("\nexit\n");
			out.flush();
			exitVal = proc.waitFor();
			if (exitVal == 0) {
				waptimeActivity.showMsg("矫正时间成功！");
				internetTime_DC.setDeviation_time_millis(0l);//矫正后删除掉时间差。
				Log.e("Debug", "Successfully to su");
			}
			
		} catch (Exception e) {
			waptimeActivity.showMsg("无法得到ROOT权限！");
		}
		//http://www.cnblogs.com/TerryBlog/archive/2010/09/10/1823103.html
		//SystemClock.setCurrentTimeMillis(time);
		
	}

}
